﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace NoteHelper.BaseControl
{
    public partial class AttributesList : UserControl
    {
        public AttributesList()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 添加 
            var arr = textBox1.Text.Split(' ');
            foreach (string note in arr)
            {
                if (string.IsNullOrWhiteSpace(note))
                    continue;
                if (!Attributes.Contains(note))
                    listBox1.Items.Add(note);
            }
            textBox1.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 删除
            if (listBox1.SelectedIndex != -1)
            {
                int index = listBox1.SelectedIndex;
                listBox1.Items.RemoveAt(index);

                int count = listBox1.Items.Count;
                if (count > 0)
                {
                    listBox1.SelectedIndex = index < count ? index : count - 1;
                }
            }
        }

        public List<string> Attributes
        {
            get
            {
                var lst = new List<string>();
                foreach (var item in listBox1.Items)
                {
                    lst.Add(item.ToString());
                }
                return lst;
            }
            set
            {
                listBox1.Items.Clear();
                listBox1.Items.AddRange(value.ToArray());
            }
        }
    }
}
